package com.sgai.zero_robot.ui.history;

import android.text.TextUtils;


import com.sgai.zero_robot.database.ZeroDetectionEntity;
import com.sgai.zero_robot.database.ZeroDetectionRepository;
import com.sgai.zero_robot.pojo.BunchInfo;
import com.sgai.zero_robot.pojo.InstallInfo;
import com.sgai.zero_robot.pojo.PhaseInfo;

import java.util.ArrayList;
import java.util.List;

public class LoadRunnable implements Runnable {
    private final RunnableCallBack<List<InstallInfo>> callBack;
    private final ZeroDetectionRepository repository;

    public LoadRunnable(ZeroDetectionRepository repository, RunnableCallBack<List<InstallInfo>> callBack){
        this.callBack=callBack;
        this.repository=repository;
    }
    @Override
    public void run() {
        List<ZeroDetectionEntity> list = repository.getAll();
        if (list == null || list.size() <= 0) {
            if (callBack!=null){
                callBack.onResult(null);
            }
            return;
        }
//        List<ZeroDetectionEntity> list = Test.getTest();
        List<InstallInfo>  installInfos = new ArrayList<>();
        for (ZeroDetectionEntity zeroDetectionEntity:list){
            handleInstallInfo(installInfos,zeroDetectionEntity);
        }
        //将数据塞入大的字典中
        if (callBack!=null){
            callBack.onResult( installInfos);
        }
    }
    private void handleInstallInfo( List<InstallInfo>  installInfos,ZeroDetectionEntity zeroDetectionEntity){
        InstallInfo installInfo = hasInstallInfo(installInfos, zeroDetectionEntity);
        if (installInfo == null){
            installInfo = new InstallInfo();
            installInfo.install = zeroDetectionEntity.getInstall();
            installInfo.lineName = zeroDetectionEntity.getLineName();
            installInfo.towerName = zeroDetectionEntity.getTowerNum();
            installInfo.province = zeroDetectionEntity.getProvince();
            installInfo.city = zeroDetectionEntity.getCity();
            installInfos.add(installInfo);
        }
        if (installInfo.phaseInfos == null){
            installInfo.phaseInfos = new ArrayList<>();
        }
        PhaseInfo phaseInfo = hasPhase(installInfo.phaseInfos,zeroDetectionEntity);
        if (phaseInfo == null){
            phaseInfo = new PhaseInfo();
            phaseInfo.phase = zeroDetectionEntity.getPhase();
            installInfo.phaseInfos.add(phaseInfo);
        }
        if (phaseInfo.insulatorInfos == null){
            phaseInfo.insulatorInfos = new ArrayList<>();
        }
        BunchInfo bunchInfo = hasBunch(phaseInfo.insulatorInfos, zeroDetectionEntity);
        if (bunchInfo == null){
            bunchInfo = new BunchInfo();
            bunchInfo.insulator = zeroDetectionEntity.getInsulator();
            phaseInfo.insulatorInfos.add(bunchInfo);
        }
        if (bunchInfo.insulatorInfos == null){
            bunchInfo.insulatorInfos = new ArrayList<>();
        }
        bunchInfo.insulatorInfos.add(zeroDetectionEntity);
    }
    private BunchInfo hasBunch(List<BunchInfo> insulatorInfos, ZeroDetectionEntity zeroDetectionEntity){
        if (insulatorInfos == null||insulatorInfos.size()<=0){
            return null;
        }
        for (BunchInfo bunchInfo:insulatorInfos){
            if (bunchInfo.insulator == zeroDetectionEntity.getInsulator()){
                return bunchInfo;
            }
        }
        return null;
    }
    private PhaseInfo hasPhase(List<PhaseInfo> phaseInfos, ZeroDetectionEntity zeroDetectionEntity){
        if (phaseInfos == null||phaseInfos.size()<=0){
            return null;
        }
        for (PhaseInfo phaseInfo:phaseInfos){
            if (phaseInfo.phase == zeroDetectionEntity.getPhase()){
                return phaseInfo;
            }
        }
        return null;
    }
    private InstallInfo hasInstallInfo(List<InstallInfo>  installInfos,ZeroDetectionEntity zeroDetectionEntity){
        if (installInfos == null||installInfos.size()<=0){
            return null;
        }
        for (InstallInfo installInfo:installInfos){
            if (!TextUtils.isEmpty(installInfo.lineName)&&installInfo.lineName.equals(zeroDetectionEntity.getLineName())&&
            installInfo.towerName.equals(zeroDetectionEntity.getTowerNum())&&installInfo.install==zeroDetectionEntity.getInstall()){
                return installInfo;
            }
        }
        return null;
    }
}